<?php
// 订单类

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
	
	// 订单查询页
    public function index()
    {
		// 订单查询页
        return view("Admin.order.list");
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
	// 查询主订单
    public function show( Request $request )
    {
		$data = $request -> except('_token');
		$keyword = $data['keyword'];
		// 11位是电话
		$ptn='/^\d{11}$/';
		preg_match_all($ptn,$keyword,$arr);	
		if( count($arr,true)>1 )
		{
			// 是电话
			$one = 'o.phone';
			$two = '=';
			$three = $keyword;
		}else
		{
			// 1到10位是订单号
			$ptn='/^\d{1,10}$/';
			preg_match_all($ptn,$keyword,$res);
			if( count($res,true)>1 )
			{
				// 是订单号
				$one = 'o.id';
				$two = '=';
				$three = $keyword;
			}else
			{
				// 是 收件人 姓名
				$one = 'o.username';
				$two = 'like';
				$three = '%'.$keyword.'%';
			}
			
		}
		// 定义订单状态数组 
		$status = array( 1=>'生成订单未付款',2=>'取消',3=>'已付款待发货',4=>'待收货',5=>'已完成' );
		// 定义是否付款数组
		$is_paid = array( 1=>'已支付',2=>'未付' );
		// 付款方式
		$all_pay = DB::table('payment')->select('name')->get();
		$pay = array( 1=>'已支付',2=>'未付' );
		
		// 查询 主订单 详情 
		$all_order = DB::table('order as o')->where( $one,$two,$three )->get();
		// 组装格式 返回前台
		$html = '';
		foreach( $all_order as $order_info ){
			$html .= "<tr action='open' isLoad='false' id='tr_".$order_info->id."' >";
			$html .= "<td>".$order_info->id."</td>";
			$html .= "<td>".$order_info->username."</td>";
			$html .= "<td>".$order_info->phone."</td>";
			$html .= "<td>".$order_info->addr."</td>";
			$html .= "<td>".$order_info->car."</td>";
			$html .= "<td>".$order_info->need_to_pay."</td>";
			$html .= "<td>".$is_paid[$order_info->is_paid]."</td>";
			$html .= "<td>".$all_pay[$order_info->pay_id-1]->name."</td>";
			$html .= "<td>".$status[$order_info->status]."</td>";
			$html .= "<td><button onclick='detail(".$order_info->id.")'  style='background:rgba(0,0,0,0);border:0px solid red;'>订单详情";
			$html .= "<span class='glyphicon glyphicon-chevron-down' aria-hidden='true'   ></span></button></td></tr>";
		}
		if( $html=='' )
		{
			$html='<h2>搜索不到相关数据</h2>';
		}
		exit( $html );
    }
	
	// 查询订单详情
	public function getDetailById( $id )
	{
		
		// 查询订单
		$all_order = DB::table('order as o')
					-> select( 'c.id','c.name','odr.cnt','odr.price','cp.pic_url' )
					-> leftJoin('order_detail as odr','odr.oid','=','o.id')
					-> leftJoin('com_pic as cp','cp.sku','=','odr.sku')
					-> leftJoin('com as c','c.id','=','odr.sku')
					-> where( 'o.id','=',$id )
					-> where( 'cp.type','=','1' )
					-> get();
		// 组装格式 返回前台
		$html='';
		$html .= "<tr class='row' style='display:none' >";
			$html .= "<td>商品</td>";
			$html .= "<td>预览</td>";
			$html .= "<td>数量</td>";
			$html .= "<td>价格</td>";
			$html .= "<td>小计</td>";
		$html .= "</tr>";
		foreach( $all_order as $order_info ){
			$html .= "<tr class='row' style='display:none' >";
			$html .= "<td>".$order_info->name."</td>";
			$html .= "<td><a target='_blank' href='/comDetail/".$order_info->id."' ><img src='/images/coms/".$order_info->pic_url."' style='width:100px;' /></a></td>";
			$html .= "<td>".$order_info->cnt."</td>";
			$html .= "<td>".$order_info->price."</td>";
			$html .= "<td>".$order_info->price*$order_info->cnt."</td></tr>";
		}
		exit( $html );
	}

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
	
	
}
